<script>
	import show_modal_fun from '@/utils/modal/modal.js'
	import md5 from 'js-md5'
	let Base64 = require('js-base64').Base64
	let os = uni.getStorageSync('osName')
	let timer;
	let timestamp;
	import http from '@/utils/https.js'
	export default {
		globalData: {
			selectiveTechnician: {}, //被选择技师
			selectiveTime: {}, //选择的时间信息 
			massageOrderInfo: {}, //按摩订单信息
			selectVoucher: {}, //选中的代金券信息
			noticeData: {} //公告
		},
		methods: {
			modal(content, callback = function() {
				uni.navigateBack()
			}, title = '', delCancel = true) {
				if (os == "android") {
					show_modal_fun({
						title: title, //可选,不填则不显示
						content: content,
						confirmVal: '确定', // 可选
						delCancel: delCancel
					}).then(res => {
						callback()
					}).catch(res => {})
				} else {
					uni.showModal({
						title: title,
						content: content,
						showCancel: !delCancel,
						success(res) {
							if (res.confirm) {
								callback()
							}
						}
					})
				}
			},
			/**
			 * 生成请求签名
			 */
			createSign() {
				let timestamp = Date.parse(new Date()) / 1000
				let last_num = timestamp.toString().slice(-1)
				if (last_num <= 3) {
					last_num = 3;
				} else if (last_num > 3 && last_num <= 6) {
					last_num = 6;
				} else if (last_num > 6 && last_num <= 9) {
					last_num = 9;
				} else {
					last_num = 0;
				}
				let new_str = timestamp.toString().slice(0, 9)
				let md5_str = md5(new_str);
				let str_split = md5_str.split('')
				let new_str_split = str_split.sort()
				return Base64.encode(new_str_split.join("") + '&time=' + timestamp);
			},
			openChangeLocation() {
				let that = this
				let i = 10
				uni.startLocationUpdate({
					success: () => {
						uni.onLocationChange(function(res) {
							i++
							if (i % 10 == 0) {
								if (i > 10000) {
									i = 0
								}
								uni.request({
									url: http.baseUrl +
										'/technician.technician/setTechnicianLocation',
									method: "POST",
									header: {
										token: uni.getStorageSync('token'),
										'X-Requested-With': 'XMLHttpRequest',
										'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
										'sign': this.createSign(),
									},
									data: {
										lng: res.longitude,
										lat: res.latitude,
									},
									success(data) {
										// console.log(data);
									}
								})
							}
						});
					},
					fail: err => {

					}
				})
			},

			onShowGetLocation() {
				let that = this
				let lat = this.$store.state.userLocation.latitude
				let lng = this.$store.state.userLocation.longitude

				if (!lat || !lng) {
					uni.request({
						url: http.baseUrl + '/index/getLocationByIp',
						method: "POST",
						header: {
							token: uni.getStorageSync('token'),
							'X-Requested-With': 'XMLHttpRequest',
							'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
							'sign': this.createSign(),
						},
						data: {},
						success(res) {
							if (res.data.code == 0) {
								that.request.httpTokenRequest({
									url: "movie/getCityCode",
									method: "get"
								}, {
									name: res.data.data.city
								}).then(function(data) {
									if (data.code == 0) {
										that.$store.commit('changeLocation', {
											cityCode: data.data.cityCode,
											cityName: data.data.cityName,
											regionCode: "",
											regionName: "",
											longitude: res.data.data.lng,
											latitude: res.data.data.lat,
										})
										let location = {
											longitude: res.data.data.lng,
											latitude: res.data.data.lat,
											province: res.data.data.province,
											city: res.data.data.city,
											cityname: res.data.data.city
										}
										that.$store.commit('changeUserLocation', location)



										// 首页上报用户地址信息

									} else {}
								})
							} else {}
						}
					})
				}
			},
		},
		onLaunch: function() {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation("portrait-primary")


			// cid
			uni.getPushClientId({
				success: res => {
					uni.setStorageSync('cid', res.cid)
					if (uni.getStorageSync('token')) {
						uni.getPushClientId({
							success: res => {
								uni.request({
									url: http.baseUrl + '/user/edit',
									method: "POST",
									header: {
										token: uni.getStorageSync('token'),
										'X-Requested-With': 'XMLHttpRequest',
										'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
										'sign': this.createSign(),
									},
									data: {
										cid: res.cid
									},
									success(data) {}
								})
							}
						})
					}
				}
			})

			// #endif

			// #ifdef APP-PLUS
			uni.getSystemInfo({
				success: res => {
					if (res.osName == 'ios') {
						plus.device.getInfo({ //需要勾选IDFA
							success: res => {
								uni.request({
									url: http.baseUrl + '/index/checkIdfa',
									method: "POST",
									header: {
										token: uni.getStorageSync('token'),
										'X-Requested-With': 'XMLHttpRequest',
										'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
										'sign': this.createSign(),
									},
									data: {
										idfa: res.idfa
									},
									success(data) {
										// console.log(data.data,'idfa');
									}
								})

							},
							fail: function(e) {
								console.log('getDeviceInfo failed: ' + JSON.stringify(e));
							}
						});
					}
					uni.setStorageSync('osName', res.osName)
				}
			})
			// #endif
			// 申请新增地址
			uni.setStorageSync('is_Address', false)

			// 技师定位
			if (uni.getStorageSync('user') && uni.getStorageSync('user').is_service == 1) {
				let os = uni.getStorageSync('osName')
				if (os == 'ios') {
					this.openChangeLocation()
				} else {
					if (uni.getStorageSync('LOCATION')) {
						this.openChangeLocation()
					} else {
						this.modal('将获取您的位置信息，向用户展示您的当前位置', () => {
							uni.setStorageSync('LOCATION', true)
							this.openChangeLocation()
						})
					}
				}
			}



		},
		onShow: function() {
			this.onShowGetLocation()
			// console.log('show');
			// #ifdef APP
			plus.runtime.setBadgeNumber(0)
			// #endif
		},
		onHide: function() {
			// console.log('App Hide')
		},
		onExit() {
			uni.stopLocationUpdate()
			uni.offLocationChange()
		},
	}
</script>

<style lang="scss">
	@import url("common/css/page.scss");
	@import url("colorui/main.css");
	@import url("colorui/icon.css");
	@import './static/iconfont/iconfont.css';
	@import './static/saoba/iconfont.css';
	/*#ifdef H5*/
	uni-page {
		height: auto;
	}

	uni-page-head {
		display: none;
	}

	/*#endif*/
	page {
		background-color: #F5F7F8;
	}

	.box-content {
		padding: 0 24rpx;
	}
	
	button::after {
		border: 0;
	}

	.dark {
		filter: grayscale(100%);
	}

	.index-page {
		background: url('https://qiniu-cdn.maeiyun.com/imgs/indexbg.png')no-repeat;
		background-size: 100% 100%;
		height: 100vh;
		padding: 80rpx $uni-padding-col-base 130rpx $uni-padding-col-base;
	}

	.flex_r_between {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.flex_r_around {
		display: flex;
		align-items: center;
		justify-content: space-around;
	}

	.flex_c {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.no_border {
		border: 0 !important;
	}

	.hidden {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
</style>